package com.navinfo.app.hd

import android.content.DialogInterface
import android.content.Intent
import android.os.*
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.navinfo.collect.library.data.RealmUtils

class LoadingActivity: AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.loading_activity)
        // 申请权限
        requestPermission()
    }

    private fun requestPermission() {
        XXPermissions.with(this)
            // 申请单个权限
            .permission(Permission.ACCESS_COARSE_LOCATION)
            .permission(Permission.ACCESS_FINE_LOCATION)
            .permission(Permission.READ_EXTERNAL_STORAGE)
            .permission(Permission.WRITE_EXTERNAL_STORAGE)
            .permission(Permission.REQUEST_INSTALL_PACKAGES)
            // 设置权限请求拦截器（局部设置）
            //.interceptor(new PermissionInterceptor())
            // 设置不触发错误检测机制（局部设置）
            //.unchecked()
            .request(object : OnPermissionCallback {

                override fun onGranted(permissions: MutableList<String>, all: Boolean) {
                    if (all) {
                        // 初始化Realm
                        RealmUtils.getInstance().init(applicationContext, null,"data-realm.realm")

                        Toast.makeText(this@LoadingActivity, "获取所有权限成功", Toast.LENGTH_SHORT).show()
                        val intent = Intent(this@LoadingActivity, MainActivity::class.java)
                        startActivity(intent)
                        this@LoadingActivity.finish()
                    } else {
                        Toast.makeText(this@LoadingActivity, "获取权限成功：$permissions", Toast.LENGTH_SHORT).show()
                    }
                    if (!permissions.contains(Permission.READ_EXTERNAL_STORAGE)) {
                        val dialogBuilder = MaterialAlertDialogBuilder(this@LoadingActivity)
                            .setTitle("提醒")
                            .setMessage("您拒绝了读取SD卡的权限，程序将无法正常！")
                            .create()


                        dialogBuilder.setButton(DialogInterface.BUTTON_POSITIVE, "确定", object: DialogInterface.OnClickListener {
                            override fun onClick(p0: DialogInterface?, p1: Int) {
                                // 如果是被永久拒绝就跳转到应用权限系统设置页面
                                XXPermissions.startPermissionActivity(this@LoadingActivity, permissions)
                            }
                        })
                        dialogBuilder.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", Message())
                        dialogBuilder.show()
                    }
                }

                override fun onDenied(permissions: MutableList<String>, never: Boolean) {
                    if (never) {
                        val dialogBuilder = MaterialAlertDialogBuilder(this@LoadingActivity)
                            .setTitle("提醒")
                            .setMessage("您拒绝了$permissions 等权限，点击确定在设置中手动授权！")
                            .create()


                        dialogBuilder.setButton(DialogInterface.BUTTON_POSITIVE, "确定", object: DialogInterface.OnClickListener {
                            override fun onClick(p0: DialogInterface?, p1: Int) {
                                // 如果是被永久拒绝就跳转到应用权限系统设置页面
                                XXPermissions.startPermissionActivity(this@LoadingActivity, permissions)
                            }
                        })
                        dialogBuilder.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", Message())
                        dialogBuilder.show()
                    } else {
                        Toast.makeText(this@LoadingActivity, "拒绝授予权限：$permissions", Toast.LENGTH_SHORT).show()
                    }
                }
            })
    }
}